﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using Model;
using System.IO;
using System.Drawing.Imaging;
using ServiceCenterApp.Helper;

namespace ServiceCenterApp
{
    public partial class FormAddProduct : XtraForm, IChildForm
    {
        ProductBLL productBLL = new ProductBLL();
        CategoryBLL categoryBLL = new CategoryBLL();
        KeyBLL keyBLL = new KeyBLL();
        List<Category> categoryList = new List<Category>();
        List<Key> keyList = new List<Key>();
        ProductListView product = null;
        bool isCreate = true;

        public FormAddProduct()
        {
            InitializeComponent();
        }

        public void ShowEdit(ProductListView p)
        {
            this.product = p;
            this.txtName.Text = p.Name;
            this.txtPrice.Text = p.Price.ToString();
            this.lueCategory.EditValue = p.CategoryList;
            this.lueKey.EditValue = p.KeyList;
            if (product.IndexImageArray != null)
            {
                this.picImage.Image = Image.FromStream(new MemoryStream(product.IndexImageArray));
            }
            this.richDetail.RtfText = p.Detail;
            this.richSummary.RtfText = p.Summary;
            this.Show();
        }

        private void LoadData()
        {
            categoryList.Clear();
            categoryList.AddRange(categoryBLL.GetAllCategory());
            this.categoryBindingSource.DataSource = categoryList;
            keyList.Clear();
            keyList.AddRange(keyBLL.GetAllKeys());
            this.keyBindingSource.DataSource = keyList;
        }

        public void Save()
        {
            button1_Click(null, null);
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public void RefreshData()
        {
            throw new NotImplementedException();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.picImage.Image = Image.FromFile(openFileDialog1.FileName);
            }
        }

        //save product
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtName.Text) || string.IsNullOrEmpty(this.txtPrice.Text))
            {
                Program.ShowValidateInfo("请输入名称和价格！"); return;
            }
            decimal price = 0;
            if (!decimal.TryParse(this.txtPrice.Text, out price))
            {
                Program.ShowValidateInfo("价格输入错误！"); this.txtPrice.SelectAll(); return;
            }
            if (product == null)
            {
                product = new ProductListView();
            }
            product.Name = this.txtName.Text;
            product.CategoryList = this.lueCategory.EditValue.ToString();
            product.KeyList = this.lueKey.EditValue.ToString();
            product.Price = price;
            product.Detail = this.richDetail.RtfText;
            product.Summary = this.richSummary.HtmlText;
            if (picImage.Image != null&&string.IsNullOrEmpty(product.IndexImage))
            {
                product.IndexImage = Guid.NewGuid().ToString() + ".jpg";
            }
            try
            {
                productBLL.Save(product);
                if (picImage.Image != null)
                {
                    using (FileStream fileStream = new FileStream(AppHelper.ImageRootPath + product.IndexImage, FileMode.Create))
                    {
                        picImage.Image.Save(fileStream, ImageFormat.Jpeg);
                        product.ImageChanged = false;
                    }
                }
                this.Text = "修改产品";
            }
            catch (Exception ex)
            {
                Program.ShowError(ex);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void FormAddProduct_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void lueCategory_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
